package com.sjl.web;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjl.entity.Sections;
import com.sjl.service.SectionsService;
import com.sjl.service.TeachersService;
import com.sjl.util.Result;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sjl
 * @since 2020-11-01
 */
@RestController
@RequestMapping("/sections")
public class SectionsController {

	@Resource
	private SectionsService sectionsServiceSjl;
	@Resource
	private TeachersService teachersServiceSjl;

	@RequestMapping("/getAll")
	public Object selectAll() {
		return sectionsServiceSjl.list(null);
	}

	@RequestMapping("/selectSectionsSjl")
	public Object selectSectionsSjl(Sections sections, Page<Sections> page) {
		return sectionsServiceSjl.selectByMemPage(page, sections);
	}

	@RequestMapping("/getAllVue")
	public Object getAllVue(Page<Sections> page) {
		return new Result(0, "成功", sectionsServiceSjl.page(page), sectionsServiceSjl.count());
	}

	@RequestMapping("createSectionsSjl")
	public Object createSectionsSjl(Sections sections) {

		Sections sectionName = sectionsServiceSjl
				.getOne(new QueryWrapper<Sections>().eq("sections_name", sections.getSectionsName()));
		if (sectionName != null) {
			return new Result(1, "添加失败，该名称已存在！");
		}

		boolean i = sectionsServiceSjl.save(sections);
		if (i == true) {
			return new Result(0, "添加成功");
		}
		return new Result(1, "添加失败");
	}

	@RequestMapping("updateSectionsSjl")
	public Object updateSections(Sections sections) {
		Sections sectionName = sectionsServiceSjl
				.getOne(new QueryWrapper<Sections>().eq("sections_name", sections.getSectionsName()));
		if (sectionName != null && !sectionName.getSectionsId().equals(sections.getSectionsId())) {
			return new Result(1, "修改失败，该名称已存在！");
		}
		boolean bl = sectionsServiceSjl.updateById(sections);
		if (bl == true) {
			return new Result(0, "修改成功！");
		}
		return new Result(1, "修改失败！");
	}

	@RequestMapping("deleteSectionsSjl")
	public Object deleteSectionsSjl(Integer sectionsId) {

		boolean bl = sectionsServiceSjl.removeById(sectionsId);
		if (bl) {
			return new Result(0, "删除成功！");
		}
		return new Result(1, "删除失败！");
	}
}
